<?php
class youkumovie
{
	public $ShowPageUrl = '';
	public $ShowPageId = '';
	public $MovieName = '';
	public $Path = array();
	public $VShow = '';
	public $Rs = array();
	public $YoukuDomain = 'http://www.youku.com/';
	public $Rewrite = 0;

	public function __construct($ShowPageUrl, $MovieName, $Path = '', $Rewrite = 0)
	{
		$this->ShowPageUrl = $ShowPageUrl;
		if(preg_match('/id_([0-9a-zA-Z]+)\.html$/', $ShowPageUrl, $Matches))
		{
			$this->ShowPageId = $Matches[1];
		}
		$MovieName = str_replace(':', ' ', $MovieName);
		$this->MovieName = $MovieName;
		$this->Rewrite = $Rewrite;
		$this->Rs['moviename'] = $MovieName;
		$this->Rs['copyfrom'] = '|1';//优酷
		$this->Path['root'] = $Path ? $Path : PHPCMS_PATH."uploadfile/";
		$this->makeDir();
	}

	public function makeDir()
	{
		if($this->Path['root'])
		{
			if(!file_exists($this->Path['root']))
			{
				mkdir($this->Path['root'], "0644", true);
			}
			//			$this->Path['html'] = $this->Path['root']."html/movie/".$this->MovieName."/";
			//			if(!file_exists($this->Path['html']))
			//			{
			//				mkdir($this->Path['html'], "0644", true);
			//			}
			$this->Path['actor'] = $this->Path['root']."image/actor/";
			if(!file_exists($this->Path['actor']))
			{
				mkdir($this->Path['actor'], "0644", true);
			}
			$this->Path['movie'] = $this->Path['root']."image/movie/".$this->MovieName."/";
			if(!file_exists($this->Path['movie']))
			{
				mkdir($this->Path['movie'], "0644", true);
			}
		}
	}

	public function getPage($Url, $FileName = "", $Rewrite = 0, $FileSize = 1100)
	{
		$ch = curl_init();
		if(empty($FileName))
		{
			preg_match('/(id_[0-9a-zA-Z]+\.html)$/', $Url, $Matches);
			$FileName = $this->Path['html'].$Matches[1];
		}
		else
		{
			$FileName = $this->Path['html'].$FileName;
		}
		if(!file_exists($FileName) || $Rewrite || (file_exists($FileName) && filesize($FileName) <= $FileSize) )
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);
			
			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function getPageContent($Url)
	{
		if($Url)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);			
			
			$html = curl_exec($ch);
			curl_close($ch);

			return $html;
		}
	}

	public function getActorAvatar($Url, $FileName = "", $Type = "jpg")
	{
		$ch = curl_init();

		if(empty($FileName))
		{
			preg_match('/\/([-0-9a-zA-Z]+)$/', $Url, $Matches);
			$FileName = $this->Path['actor'].$Matches[1].".".$Type;
		}
		else
		{
			if(strpos($FileName, '•') !== false)
			{
				$FileName = str_replace('•', '', $FileName);
				$FileName = $this->Path['actor'].iconv("UTF-8", "GBK", $FileName).".".$Type;
			}
			else
			{
				$FileName = $this->Path['actor'].iconv("UTF-8", "GBK", $FileName).".".$Type;
			}
		}
		if(!file_exists($FileName) || (file_exists($FileName) && filesize($FileName) <= 500))
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);
			
			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function getMoviePic($Url, $FileName = "", $IsGBK = true, $Type = "jpg")
	{
		$ch = curl_init();
		if(empty($FileName))
		{
			preg_match('/\/([-0-9a-zA-Z]+)$/', $Url, $Matches);
			$FileName = $this->Path['movie'].$Matches[1].".".$Type;
		}
		else
		{
			if($IsGBK)
			{
				$FileName = $this->Path['movie'].iconv("UTF-8", "GBK", $FileName).".".$Type;
			}
			else
			{
				$FileName = $this->Path['movie'].$FileName.".".$Type;
			}
		}
		if(!file_exists($FileName) || (file_exists($FileName) && filesize($FileName) <= 1100))
		{
			$fp = fopen($FileName, "w");

			curl_setopt($ch, CURLOPT_URL, $Url);
			curl_setopt($ch, CURLOPT_FILE, $fp);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60);
			
			curl_exec($ch);
			curl_close($ch);
			fclose($fp);
		}
		return $FileName;
	}

	public function parseShowPage()
	{
		$Content = $this->getPageContent($this->ShowPageUrl);
		$Title = substr($Content, strpos($Content, '<title>') + 7, strpos($Content, '</title>')-strpos($Content, '<title>')-7);
		$this->Rs['title'] = substr($Title, 0, strpos($Title, ' -'));
		$KeyWords = array();
		$KeyWords[] = $this->Rs['title'];
		//$this->Rs['keywords'] = $this->Rs['title'];
		if(preg_match('/\s+时长([^\s]*)\s+/', $Title, $Matches))
		{
			$this->Rs['duration'] = $Matches[1];
		}
		$Year = substr($Content, strpos($Content, '<span class="pub">')+18, 4);
		$this->Rs['year'] = $Year;

		if(preg_match('/<a[\w\W]*?charset="412-3-5-[^>]*>([^<]*)<\/a>/', $Content, $Matches))
		{
			$this->Rs['area'] = $Matches[1];
		}
		if(preg_match_all('/<a[\w\W]*?charset="412-3-6-[^>]*>([^<]*)<\/a>/', $Content, $Matches))
		{
			$this->Rs['movietype'] = implode(',', $Matches[1]);
		}
		if(preg_match('/<em class="num">([^<]*)<\/em>/', $Content, $Matches))
		{
			$this->Rs['rate'] = $Matches[1];
		}

		if(preg_match('/<a charset="412-1-8"[^>]*><img src="([^"]*)"[^>]*?><\/a>/', $Content, $Matches) && (!isset($this->Rs['moviepic'])||empty($this->Rs['moviepic'])))
		{
			$this->getMoviePic($Matches[1], $this->MovieName, false);
			$UploadUrl = pc_base::load_config('system','upload_url');
			$this->Rs['thumb'] = $UploadUrl."image/movie/".$this->MovieName."/".$this->MovieName.".jpg";
			$this->Rs['moviepic'] = $this->Rs['thumb'];
		}
		else if(preg_match('/<a charset="412-1-9"[^>]*><img src="([^"]*)"[\w\W]*?\/><\/a>/', $Content, $Matches))
		{
			$this->getMoviePic($Matches[1], $this->MovieName, false);
			$UploadUrl = pc_base::load_config('system','upload_url');
			$this->Rs['thumb'] = $UploadUrl."image/movie/".$this->MovieName."/".$this->MovieName.".jpg";
			$this->Rs['moviepic'] = $this->Rs['thumb'];
		}

		if(preg_match('/<span[\s]+id="show_info_short"[^>]*?>([\w\W]*?)<\/span>/', $Content, $Matches))
		{
			$this->Rs['description'] = $Matches[1];
		}
		else if(preg_match('/<li[\s]+class="statdesc">[\s]*?<span>([\w\W]*?)<\/span>[\s]+<\/li>/', $Content, $Matches))
		{
			$this->Rs['description'] = $Matches[1];
		}
		if(preg_match('/<span[\s]+id="show_info_long"[^>]*?>([\w\W]*?)<\/span>/', $Content, $Matches))
		{
			$this->Rs['content'] = '<div class="v_story">'.$Matches[1].'</div><p>[page]</p><div class="v_story"></div>';
			if(!isset($this->Rs['description']) || empty($this->Rs['description']))
			{
				$this->Rs['description'] = mb_substr($Matches[1], 0, 255, 'UTF-8');
			}
		}
		else
		{
			$this->Rs['content'] = '<div class="v_story">'.$this->Rs['description'].'</div><p>[page]</p><div class="v_story"></div>';
		}

		if(preg_match('/<h6>演员:<\/h6>\s+<div\s+class="users">([\w\W]*?)<\/div><!--\.users-->/', $Content, $Matches))
		{
			if(preg_match_all('/<img\s+src="([\w\W]*?)"\s+alt="([\w\W]*?)"\s+\/>/', $Matches[1], $Matches2))
			{
				if(isset($Matches2[2]))
				{
					$Actors = str_replace(array('　'), array(''), $Matches2[2]);
					$this->Rs['actor'] = ','.implode(',', $Actors).',';
					$Actor = array_slice($Actors, 0, 4);
					$KeyWords = array_merge($KeyWords, $Actor);

				}

				if(isset($Matches2[1]) && is_array($Matches2[1]))
				{
					foreach($Matches2[1] as $Key => $Val)
					{
						$this->getActorAvatar($Val, $Matches2[2][$Key]);
					}
				}
			}

			if(preg_match_all('/<li class="portray">\s+([\w\W]*?)\s+<\/li>/', $Matches[1], $Matches2))
			{
				if(isset($Matches2[1]))
				{
					$Roles = str_replace(array('　'), array(''), $Matches2[1]);
					$this->Rs['role'] = ','.implode(',', $Roles).',';
					$Role = array_slice($Roles, 0, 4);
					$KeyWords = array_merge($KeyWords, $Role);
				}
			}
		}

		if(preg_match('/<h6>导演:<\/h6>\s+<div\s+class="users">([\w\W]*?)<\/div><!--\.users-->/', $Content, $Matches))
		{
			if(preg_match_all('/<img\s+src="([\w\W]*?)"\s+alt="([\w\W]*?)"\s+\/>/', $Matches[1], $Matches2))
			{
				if(isset($Matches2[2]))
				{
					$Directors = str_replace(array('　'), array(''), $Matches2[2]);
					$this->Rs['director'] = implode(',', $Directors);
					$KeyWords = array_merge($KeyWords, $Directors);

				}

				if(isset($Matches2[1]) && is_array($Matches2[1]))
				{
					foreach($Matches2[1] as $Key => $Val)
					{
						$this->getActorAvatar($Val, $Matches2[2][$Key]);
					}
				}
			}
		}

		if(preg_match('/<h6>编剧:<\/h6>\s+<div\s+class="users">([\w\W]*?)<\/div><!--\.users-->/', $Content, $Matches))
		{
			if(preg_match_all('/<img\s+src="([\w\W]*?)"\s+alt="([\w\W]*?)"\s+\/>/', $Matches[1], $Matches2))
			{
				if(isset($Matches2[2]))
				{
					$Playwrights = str_replace(array('　'), array(''), $Matches2[2]);
					$this->Rs['playwright'] = implode(',', $Playwrights);
					$KeyWords = array_merge($KeyWords, $Playwrights);
				}

				if(isset($Matches2[1]) && is_array($Matches2[1]))
				{
					foreach($Matches2[1] as $Key => $Val)
					{
						$this->getActorAvatar($Val, $Matches2[2][$Key]);
					}
				}
			}
		}

		if(preg_match('/<h6>监制:<\/h6>\s+<div\s+class="users">([\w\W]*?)<\/div><!--\.users-->/', $Content, $Matches))
		{
			if(preg_match_all('/<img\s+src="([\w\W]*?)"\s+alt="([\w\W]*?)"\s+\/>/', $Matches[1], $Matches2))
			{
				if(isset($Matches2[2]))
				{
					$Managers = str_replace(array('　'), array(''), $Matches2[2]);
					$this->Rs['manager'] = implode(',', $Managers);
					$KeyWords = array_merge($KeyWords, $Managers);
				}

				if(isset($Matches2[1]) && is_array($Matches2[1]))
				{
					foreach($Matches2[1] as $Key => $Val)
					{
						$this->getActorAvatar($Val, $Matches2[2][$Key]);
					}
				}
			}
		}

		/*
		if(preg_match_all('/<li class="name"><a charset="[^>]*>([\w\W]*?)<\/a><\/li>/', $Content, $Matches))
		{
			$Actors = str_replace(array('　'), array(''), $Matches[1]);
			$this->Rs['actor'] = ','.implode(',', $Actors).',';
			$Actor = array_slice($Actors, 0, 4);
			$KeyWords = array_merge($KeyWords, $Actor);
		}
		if(preg_match_all('/<li class="role" title="([\w\W]*?)">[\w\W]*?<\/li>/', $Content, $Matches))
		{
			$Roles = str_replace(array('　'), array(''), $Matches[1]);
			$this->Rs['role'] = ','.implode(',', $Roles).',';
			$Role = array_slice($Roles, 0, 4);
			$KeyWords = array_merge($KeyWords, $Role);
		}
		if(preg_match_all('/<li class="photo"><a[^>]*><img src="([^"]*?)" alt="([\w\W]*?)"><\/a><\/li>/', $Content, $Matches))
		{
			foreach($Matches[1] as $Key => $Val)
			{
				$this->getActorAvatar($Val, $Matches[2][$Key]);
			}
		}

		if(preg_match_all('/<a charset="412-13-1-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches))
		{
			$Directors = str_replace(array('　'), array(''), $Matches[1]);
			$this->Rs['director'] = implode(',', $Directors);
			$KeyWords = array_merge($KeyWords, $Directors);
		}
		if(preg_match_all('/<a charset="412-13-4-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches))
		{
			$Playwrights = str_replace(array('　'), array(''), $Matches[1]);
			$this->Rs['playwright'] = implode(',', $Playwrights);
			$KeyWords = array_merge($KeyWords, $Playwrights);
		}

		if(preg_match_all('/<a charset="412-13-6-[\d]+" [^>]*>([\w\W]*?)<\/a>/', $Content, $Matches))
		{
			$Managers = str_replace(array('　'), array(''), $Matches[1]);
			$this->Rs['manager'] = implode(',', $Managers);
		}
		*/
			

		$KeyWords = array_unique($KeyWords);
		$KeyWords = str_replace(array(' ', '　'), array('', ''), $KeyWords);
		$this->Rs['keywords'] = implode(' ', $KeyWords);

		if(preg_match('/<a\s+charset="412-1-6"\s+target="_blank"\s+href="([^"]*)"[^>]*?>/', $Content, $Matches))
		{
			$this->VShow = $Matches[1];
			$VContent = $this->getPageContent($this->VShow);
			if(preg_match('/<input type="text"\s+class="[^"]*"\s+id="link2"\s+value="([^"]*)"\s?>/', $VContent, $Matches))
			{
				$this->Rs['flash'] = $Matches[1];
			}
			unset($VContent);
		}
		else if(preg_match('/<a\s+href="([^"]*)"\s+charset="412-3-1-2"\s+target="_blank"[^>]*?>/', $Content, $Matches))
		{
			$this->VShow = $Matches[1];
			$VContent = $this->getPageContent($this->VShow);
			if(preg_match('/<input type="text"\s+class="[^"]*"\s+id="link2"\s+value="([^"]*)"\s?>/', $VContent, $Matches))
			{
				$this->Rs['flash'] = $Matches[1];
			}
			unset($VContent);
		}
		unset($Content);
	}
	
	public function getAndParse()
	{
		$this->parseShowPage();

		return $this->Rs;
	}
}


?>